package jdwl.hrmgrsys.action;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.core.Pager;
import com.ssh.core.QueryResult;

@Controller
@Scope("prototype")
public class PageAction<T> extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private static final int PAGE_SIZE = 10;
	private QueryResult<T> result;
	private String queryJson;
	private NoticeMsg noticeMsg;
	private LinkedHashMap<String, String> orderBy;
	public List getQueryList() {
		return result.getList();
	}
	
	protected void notice(String msgType, String msgContent) {
		setNoticeMsg(new NoticeMsg(msgType, msgContent));
	}	
	protected String getParameter(String name){
		HttpServletRequest request= ServletActionContext.getRequest();
		String value=request.getParameter(name);
		return value==null?"":value;
	}
	
	public QueryResult<T> getResult() {
		return result;
	}
	public void setResult(QueryResult<T> result) {
		this.result = result;
	}
	
	public int getPageNo() {
		HttpServletRequest request= ServletActionContext.getRequest();
		String star=request.getParameter("iDisplayStart");
		if("".equals(star)||star==null){
			String page=request.getParameter("pageNo");
			return ("".equals(page)||page==null)?1:Integer.valueOf(page);
		}
		int dispalyStart= new Integer(star);
		return dispalyStart/getiDisplayLength()+1;
	}

	public int getiDisplayLength() {
		HttpServletRequest request= ServletActionContext.getRequest();
		String size=request.getParameter("iDisplayLength");
		if("".equals(size)||size==null){
			return PAGE_SIZE;
		}
		return new Integer(size);
	}
	
	public String getQueryJson() {
		HttpServletRequest request= ServletActionContext.getRequest();
		Map map=new HashMap();
		map.put("sEcho", request.getParameter("sEcho"));
		map.put("iTotalRecords", getResult().getRecordCount());
		map.put("iTotalDisplayRecords", getResult().getRecordCount());
		map.put("list", getResult().getList());
		String str=JSONObject.fromObject(map).toString();
		return str;
	}
	public void setQueryJson(String queryJson) {
		this.queryJson = queryJson;
	}

	public LinkedHashMap<String, String> getOrderBy() {
		orderBy=new LinkedHashMap<String, String>();
		HttpServletRequest request= ServletActionContext.getRequest();
	    String sCol = request.getParameter("iSortCol_0"); 
	    String sdir = request.getParameter("sSortDir_0");
	    String col=request.getParameter("mDataProp_"+sCol);
	    if(!("".equals(col)||null==col)){
	    	orderBy.put(col, sdir);
	    }
		return orderBy;
	}

	public void setOrderBy(LinkedHashMap<String, String> orderBy) {
		this.orderBy = orderBy;
	}

	public NoticeMsg getNoticeMsg() {
		return noticeMsg;
	}

	public void setNoticeMsg(NoticeMsg noticeMsg) {
		this.noticeMsg = noticeMsg;
	}
		
}
